﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySoft.Data;
using Itfort;
namespace WanPiJie.Products
{
	/// <summary>
	/// 类目相关服务
	/// </summary>
	public class CategoryService : ICategoryService
	{
		/// <summary>
		/// 服务实例
		/// </summary>
		public static readonly CategoryService Instance;

		private DbSession dbSession;

		public CategoryService()
		{
			dbSession = new DbSession("Product");
		}

		static CategoryService()
		{
			Instance = new CategoryService();
		}

		#region 产品类目相关

		/// <summary>
		/// 添加类目信息
		/// </summary>
		/// <param name="thirdCatalogId">类目Id</param>
		/// <param name="catalogName">类目名称</param>
		public void AddCatalog(Int64 thirdCatalogId, String catalogName)
		{
			this.dbSession.Save<_Catalog>(new _Catalog
			{
				ThirdId = thirdCatalogId,
				Name = catalogName,
				AddTime = DateTime.Now
			});
		}

		/// <summary>
		/// 更新类目信息
		/// </summary>
		/// <param name="thirdCatalogId">类目Id</param>
		/// <param name="catalogName">类目名称</param>
		public void UpdateCatalog(Int64 thirdCatalogId, String catalogName)
		{
			var fields = new Field[] { _Catalog._.Name };
			var values = new Object[] { catalogName };
			var condition = _Catalog._.ThirdId == thirdCatalogId;
			this.dbSession.Update<_Catalog>(fields, values, condition);
		}

		/// <summary>
		/// 是否存在指定的类目信息
		/// </summary>
		/// <param name="thirdCatalogId">类目Id</param>
		/// <returns>true：存在，false：不存在</returns>
		public bool HasCatalog(Int64 thirdCatalogId)
		{
			return this.dbSession.Exists<_Catalog>(_Catalog._.ThirdId == thirdCatalogId);
		}

		/// <summary>
		/// 根据第三方Id获取相关类目信息
		/// </summary>
		/// <param name="thirdCatalogId">第三方类目Id</param>
		/// <returns>指定第三方Id的类目信息</returns>
		public Catalog GetCatalogByThirdId(Int64 thirdCatalogId)
		{
			return this.dbSession.Single<_Catalog>(_Catalog._.ThirdId == thirdCatalogId).Convert2<Catalog>();
		}

		/// <summary>
		/// 获取所有类目信息
		/// </summary>
		/// <returns>返回分类列表信息</returns>
		public List<Catalog> GetCatalogs()
		{
			return this.dbSession.From<_Catalog>()
				  .ToList().Convert2<Catalog>();
		}

		/// <summary>
		/// 获取指定的类目对象
		/// </summary>
		/// <param name="id">类目Id</param>
		/// <returns>指定类目Id的类目对象</returns>
		public Catalog GetCatalogById(Int64 id)
		{
			return this.dbSession.Single<_Catalog>(_Catalog._.Id == id)
				.Convert2<Catalog>();
		}
		#endregion

		#region Attribute相关

		/// <summary>
		/// 添加属性信息
		/// </summary>
		/// <param name="thirdId">第三方属性Id</param>
		/// <param name="attributeName">属性名称</param>
		public void AddAttribute(Int64 thirdId, String attributeName)
		{
			this.dbSession.Save<_Attribute>(new _Attribute
			{
				ThirdId = thirdId,
				Name = attributeName,
				AddTime = DateTime.Now,
				UpdateTime = DateTime.Now
			});
		}

		/// <summary>
		/// 更新属性信息
		/// </summary>
		/// <param name="thirdId">第三方属性Id</param>
		/// <param name="attributeName">属性名称</param>
		public void UpdateAttribute(Int64 thirdId, String attributeName)
		{
			var fields = new Field[] { _Attribute._.Name, _Attribute._.UpdateTime };
			var values = new Object[] { attributeName, DateTime.Now };
			var condition = _Attribute._.ThirdId == thirdId;
			this.dbSession.Update<_Attribute>(fields, values, condition);
		}

		/// <summary>
		/// 根据Id获取属性信息
		/// </summary>
		/// <param name="id">属性Id</param>
		/// <returns>属性对象</returns>
		public WanPiJie.Products.Attribute GetAttributeById(Int64 id)
		{
			return this.dbSession.Single<_Attribute>(_Attribute._.Id == id).Convert2<Attribute>();
		}

		/// <summary>
		/// 根据第三方Id获取获取属性对象
		/// </summary>
		/// <param name="thirdId">第三方Id</param>
		/// <returns>属性对象</returns>
		public WanPiJie.Products.Attribute GetAttributeByThirdId(Int64 thirdId)
		{
			return this.dbSession.Single<_Attribute>(_Attribute._.ThirdId == thirdId).Convert2<Attribute>();
		}

		#endregion

		#region  商品类目属性相关

		/// <summary>
		/// 添加属性至商品分类
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">类目属性Id</param>
		/// <param name="attributeName">类目属性名称</param>
		public void AddProductAttribute(Int64 catalogId, Int64 attributeId, String attributeName)
		{
			this.dbSession.Save<_ProductAttribute>(new _ProductAttribute
			{
				CatalogId = catalogId,
				AttributeId = attributeId,
				Name = attributeName,
				AddTime = DateTime.Now,
				UpdateTime = DateTime.Now
			});
		}

		/// <summary>
		/// 更新属性至商品分类
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">类目属性Id</param>
		/// <param name="attributeName">类目属性名称</param>
		public void UpdateProductAttribute(Int64 catalogId, Int64 attributeId, String attributeName)
		{
			var fields = new Field[] { _ProductAttribute._.Name, _ProductAttribute._.UpdateTime };
			var values = new Object[] { attributeName, DateTime.Now };
			var condition = _ProductAttribute._.CatalogId == catalogId && _ProductAttribute._.AttributeId == attributeId;
			this.dbSession.Update<_ProductAttribute>(fields, values, condition);
		}

		/// <summary>
		/// 是否存在指定的属性信息
		/// </summary>
		/// <param name="catalogId">第三方类目Id</param>
		/// <param name="attributeId">第三方属性Id</param>
		public bool HasProductAttribute(Int64 catalogId, Int64 attributeId)
		{
			return this.dbSession.Exists<_ProductAttribute>(_ProductAttribute._.CatalogId == catalogId && _ProductAttribute._.AttributeId == attributeId);
		}

		/// <summary>
		/// 获取指定的属性信息
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">第三方属性Id</param>
		public ProductAttribute GetProductAttribute(Int64 catalogId, Int64 attributeId)
		{
			return this.dbSession.Single<_ProductAttribute>(_ProductAttribute._.CatalogId == catalogId && _ProductAttribute._.AttributeId == attributeId).Convert2<ProductAttribute>();
		}

		/// <summary>
		/// 获取指定类目的所有属性信息
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <returns>指定类目Id的所有属性</returns>
		public List<ProductAttribute> GetProductAttributes(Int64 catalogId)
		{
			return dbSession.From<_ProductAttribute>()
				.Where(_ProductAttribute._.CatalogId == catalogId)
				.ToList().Convert2<WanPiJie.Products.ProductAttribute>();
		}

		#endregion

		#region 类目属性值相关

		/// <summary>
		/// 添加指定类目指定属性的属性值
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">属性Id</param>
		/// <param name="thirdId">第三方属性值Id</param>
		/// <param name="value">属性值名称</param>
		public void AddProductAttrVal(Int64 catalogId, Int64 attributeId, Int64 thirdId, String value)
		{
			this.dbSession.Save<_ProductAttributeValue>(new _ProductAttributeValue
			{
				AttributeId = attributeId,
				CatalogId = catalogId,
				ThirdId = thirdId,
				Value = value,
				EditTime = DateTime.Now,
				AddTime = DateTime.Now
			});
		}

		/// <summary>
		/// 更新指定类目指定属性的属性值
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">属性Id</param>
		/// <param name="thirdId">第三方属性值Id</param>
		/// <param name="value">属性值名称</param>
		public void UpdateProductAttrVal(Int64 catalogId, Int64 attributeId, Int64 thirdId, String value)
		{
			var fields = new Field[] { _ProductAttributeValue._.Value, _ProductAttributeValue._.EditTime };
			var values = new Object[] { value, DateTime.Now };
			var condition = _ProductAttributeValue._.CatalogId == catalogId && _ProductAttributeValue._.ThirdId == thirdId
				&& _ProductAttributeValue._.AttributeId == attributeId;
			this.dbSession.Update<_ProductAttributeValue>(fields, values, condition);
		}

		/// <summary>
		/// 是否存在指定的属性值
		/// </summary>
		/// <param name="catalogId">类目Id</param>
		/// <param name="attributeId">属性Id</param>
		/// <param name="thirdId">第三方属性值Id</param>
		public bool HasProductAttrVal(Int64 catalogId, Int64 attributeId, Int64 thirdId)
		{
			return this.dbSession.Exists<_ProductAttributeValue>(_ProductAttributeValue._.CatalogId == catalogId && _ProductAttributeValue._.ThirdId == thirdId
				&& _ProductAttributeValue._.AttributeId == attributeId);
		}

		/// <summary>
		/// 获取指定的属性值信息
		/// </summary>
		/// <param name="catalogId">（第三方）类目Id</param>
		/// <param name="attributeId">（第三方）属性Id</param>
		/// <param name="thirdId">（第三方）属性值Id</param>
		/// <returns>指定的属性值对象</returns>
		public ProductAttributeValue GetProductAttrVal(Int64 catalogId, Int64 attributeId, Int64 thirdId)
		{
			return this.dbSession.Single<_ProductAttributeValue>(_ProductAttributeValue._.CatalogId == catalogId && _ProductAttributeValue._.ThirdId == thirdId
				&& _ProductAttributeValue._.AttributeId == attributeId).Convert2<ProductAttributeValue>();
		}

		#endregion

		#region 导入接口


		/// <summary>
		/// 数据插入监视
		/// </summary>
		/// <typeparam name="T">插入数据类型</typeparam>
		/// <param name="oversee">监视器</param>
		/// <param name="obj">数据对象</param>
		private void InsertOversee<T>(Action<T, string> oversee, T obj)
		{
			if (oversee != null)
			{
				oversee(obj, "Insert");
			}
		}

		/// <summary>
		/// 数据更新监视
		/// </summary>
		/// <typeparam name="T">更新数据类型</typeparam>
		/// <param name="oversee">监视器</param>
		/// <param name="obj">数据对象</param>
		private void UpdateOversee<T>(Action<T, string> oversee, T obj)
		{
			if (oversee != null)
			{
				oversee(obj, "Update");
			}
		}

		/// <summary>
		/// 导入属性值信息
		/// </summary>
		/// <param name="productAttrVal">属性值对象</param>
		/// <param name="oversee">监视</param>
		public void Import(ProductAttributeValue productAttrVal, Action<ProductAttributeValue, string> oversee)
		{
			if (this.HasProductAttrVal(productAttrVal.CatalogId, productAttrVal.AttributeId, productAttrVal.ThirdId))
			{
				this.UpdateProductAttrVal(productAttrVal.CatalogId, productAttrVal.AttributeId, productAttrVal.ThirdId, productAttrVal.Value);
				this.UpdateOversee<ProductAttributeValue>(oversee, productAttrVal);
			}
			else
			{
				this.AddProductAttrVal(productAttrVal.CatalogId, productAttrVal.AttributeId, productAttrVal.ThirdId, productAttrVal.Value);
				this.InsertOversee<ProductAttributeValue>(oversee, productAttrVal);
			}
		}

				/// <summary>
		/// 导入Attribute信息
		/// </summary>
		/// <param name="attribute">attribute对象</param>
		/// <param name="oversee">监视</param>
		public void Import(Attribute attribute, Action<Attribute, string> oversee)
		{
			if (this.GetAttributeByThirdId(attribute.ThirdId) != null)
			{
				this.UpdateAttribute(attribute.ThirdId, attribute.Name);
				this.UpdateOversee<Attribute>(oversee, attribute);
			}
			else
			{
				this.AddAttribute(attribute.ThirdId, attribute.Name);
				this.InsertOversee<Attribute>(oversee, attribute);
			}
		}

		/// <summary>
		/// 导入商品属性信息
		/// </summary>
		/// <param name="productAttribute">商品属性对象</param>
		/// <param name="oversee">监视</param>
		public void Import(ProductAttribute productAttribute, Action<ProductAttribute, string> oversee)
		{
			if (this.HasProductAttribute(productAttribute.CatalogId, productAttribute.AttributeId))
			{
				this.UpdateProductAttribute(productAttribute.CatalogId, productAttribute.AttributeId, productAttribute.Name);
				this.UpdateOversee<WanPiJie.Products.ProductAttribute>(oversee, productAttribute);
			}
			else
			{
				this.AddProductAttribute(productAttribute.CatalogId, productAttribute.AttributeId, productAttribute.Name);
				this.InsertOversee<WanPiJie.Products.ProductAttribute>(oversee, productAttribute);
			}
		}

		#endregion
	}
}
